package com.oldwine.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

/**
 * @author oldwine
 * @date 2022/6/14
 */
@Component
public class SpringRabbitListener {

   /* @RabbitListener(queues = "simple.queue")
    public void listenBasicQueueMessage(String message) {
        System.out.println("Spring 消息接收者收到的消息:" + "[" + message + "]");
    }*/

    /*@RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.out.println("消费者1接收消息：" + message);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.out.println("消费者2接收消息：" + message);
        Thread.sleep(10);
    }*/

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        System.out.println("消费者1接收Fanout消息：" + message);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        System.out.println("消费者2接收Fanout消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"), //声明绑定的队列
            exchange = @Exchange(name = "oldwine.direct", type = ExchangeTypes.DIRECT), //声明绑定的交换机
            key = {"red", "blue"}  //绑定routeKey
    ))
    public void listenDirectQueue1(String message) {
        System.out.println("消费者接收到direct.queue1的消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "oldwine.direct", type = ExchangeTypes.DIRECT),
            key = {"yellow", "red"}
    ))
    public void listenDirectQueue2(String message) {
        System.out.println("消费者接收到direct.queue2的消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "oldwine.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String message) {
        System.out.println("消费者接收到topic.queue1消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "oldwine.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String message) {
        System.out.println("消费者接收到topic.queue2的消息：" + message);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenObjectQueue1(Map map) {
        System.out.println("消费者接收的消息：" + map);
    }
}
